package com.classes;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.TreeMap;

import com.mbbmap.app.manager.ConfigManager;
import com.mbbmap.app.manager.DBConnectionManager;

public class GroupUser {
	private String user_id;
	private TreeMap<String, String> info;
	
	public static TreeMap<String, String> getUserInfo(String userId){
        String connectionPool = ConfigManager.getInstance().getConnectionPool();
        DBConnectionManager connMgr = DBConnectionManager.getInstance();
        Connection conn = connMgr.getConnection(connectionPool, 10000);
        PreparedStatement statement = null;
		try {
			String sql = "select name, email from tbluser where login_id = ?";
			
			statement = conn.prepareStatement(sql);
			statement.setString(1, userId);
			
			ResultSet resultSet = statement.executeQuery();
			
			if(resultSet.next()){
				TreeMap<String, String> t = new TreeMap<String, String>();
				t.put("name", resultSet.getString("name"));
				t.put("email", resultSet.getString("email"));
				
				return t;
			}
			
			return null;
			
		} catch (Exception e){
			e.printStackTrace();
			return null;
		} finally {
        	if (statement != null) {
        		try { statement.close(); } catch (Exception e) {}
        	}
			connMgr.freeConnection(connectionPool, conn);
		}
	}
	
	public String getUserId(){
		return this.user_id;
	}
	
	public String getInfo(String key){
		if(this.info == null){
			return null;
		}
		
		if(this.info.containsKey(key)){
			return this.info.get(key);
		}
		
		return null;
	}
	
	public void setUserId( String value ){
		this.user_id = value;
	}
	
	public void setInfo( String key, String value ){
		if(this.info == null){
			this.info = new TreeMap<String, String>();
		}
		
		this.info.put(key, value);
	}
}
